package dao;

import java.util.List;

import model.Laptop;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

public class LaptopDAO {
	private final SessionFactory sf = util.NewHibernateUtil.getSessionFactory();

	@SuppressWarnings("unchecked")
	public List<Laptop> showAllProduct() {
		try {
			sf.getCurrentSession().beginTransaction();
			return sf.getCurrentSession().createCriteria(Laptop.class).list();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Laptop> showAllLaptopByAll(String key) {
		@SuppressWarnings("rawtypes")
		List laptop = sf
				.getCurrentSession()
				.createCriteria(Laptop.class)
				.add(Restrictions.or(Restrictions.like("id", "%" + key + "%"),
						Restrictions.or(Restrictions.like("name", "%" + key
								+ "%"), Restrictions.or(Restrictions.like(
								"supplierName", "%" + key + "%"),
								Restrictions.or(Restrictions.like("cpu", "%"
										+ key + "%"), Restrictions.or(
										Restrictions.like("screen", "%" + key
												+ "%"), Restrictions.or(
												Restrictions.like("graphic",
														"%" + key + "%"),
												Restrictions.like("driver", "%"
														+ key + "%"))))))))
				.list();
		return laptop;
	}

	@SuppressWarnings("unchecked")
	public List<Laptop> showAllMobileByInfor(String key) {
		@SuppressWarnings("rawtypes")
		List laptop = sf
				.getCurrentSession()
				.createCriteria(Laptop.class)
				.add(Restrictions.or(Restrictions.sqlRestriction("hdd =" + key
						+ ""), Restrictions.or(Restrictions
						.sqlRestriction("memory =" + key + ""),
						Restrictions.or(Restrictions.sqlRestriction("battery ="
								+ key + ""), Restrictions.or(Restrictions
								.sqlRestriction("purPrice =" + key + ""),
								Restrictions.or(
										Restrictions
												.sqlRestriction("salPrice ="
														+ key + ""),
										Restrictions
												.sqlRestriction("quantity ="
														+ key + "")))))))
				.list();

		return laptop;
	}

	public boolean deleteLaptop(Laptop product) {
		try {
			sf.getCurrentSession().beginTransaction();
			sf.getCurrentSession().delete(product);
			sf.getCurrentSession().getTransaction().commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean updateProduct(Laptop l) {
		try {
			sf.getCurrentSession().beginTransaction();
			sf.getCurrentSession().saveOrUpdate(l);
			sf.getCurrentSession().getTransaction().commit();
			return true;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean insertLaptopt(Laptop l) {
		try {
			sf.getCurrentSession().beginTransaction();
			sf.getCurrentSession().save(l);
			sf.getCurrentSession().getTransaction().commit();
			return true;
		} catch (Exception em) {
			em.printStackTrace();
			sf.getCurrentSession().getTransaction().rollback();
			return false;
		}
	}

	public Laptop searchLaptop(String id) {
		try {
			sf.getCurrentSession().beginTransaction();
			return (Laptop) sf.getCurrentSession().get(Laptop.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
